﻿using crm_data;
using crm_server;
using crm_utility.Pager;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace crm_web.Controllers
{
    public class CustomersController : Controller
    {
        private readonly ILogger _logger;
        private ICustomerService _customerService;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="customerService"></param>
        public CustomersController(ILogger<HomeController> logger, ICustomerService customerService)
        {
            _logger = logger;
            _customerService = customerService;
        }


        [HttpGet]
        public IActionResult Index(string customerName, int startPage = 1, int pageSize = 10)
        {
            int rowCount = 0;
            var customers = _customerService.GetListPage(customerName, null, startPage, pageSize, out rowCount);

            PageModel pageModel = new PageModel(startPage, pageSize, rowCount);
            CustomerModel model = new CustomerModel()
            {
                PageModel = pageModel,
                CustomerList = customers,
                CustomerName = customerName
            };

            return View(model);
        }


        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }
        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <param name="customer">表单数据</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                _customerService.Insert(customer);
                return RedirectToAction(nameof(Index));
            }
            return View(customer);
        }


        // GET: Customers/Edit/5
        public IActionResult Edit(Guid id)
        {
            if (string.IsNullOrWhiteSpace(id.ToString()))
            {
                return View();
            }

            var customer = _customerService.Get(id);
            if (customer == null)
            {
                return NotFound();
            }
            return View(customer);
        }

        // POST: Customers/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(string guuid, [Bind("RealName,Sex,Birthday,PhoneNumber,ContactNumber,CutomerState,DelFlag,CreateTime,UpdateTime,OnlyGuuid")] Customer customer)
        {
            if (guuid.Equals(customer.Id.ToString()))
            {
                return View();
            }

            if (ModelState.IsValid)
            {
                //try
                //{
                //    _customerService.Update(customer);
                //    await _context.SaveChangesAsync();
                //}
                //catch (DbUpdateConcurrencyException)
                //{
                //    if (!CustomerExists(customer.Id.ToString()))
                //    {
                //        return NotFound();
                //    }
                //    else
                //    {
                //        throw;
                //    }
                //}
                return RedirectToAction(nameof(Index));
            }
            return View(customer);
        }


        //// GET: Customers/Details/5
        //public async Task<IActionResult> Details(string guuid)
        //{
        //    if (string.IsNullOrWhiteSpace(guuid))
        //    {
        //        return NotFound();
        //    }

        //    var customer = await _context.Customers
        //        .FirstOrDefaultAsync(m => m.Id.ToString() == guuid);
        //    if (customer == null)
        //    {
        //        return NotFound();
        //    }

        //    return View(customer);
        //}





        //// GET: Customers/Delete/5
        //public async Task<IActionResult> Delete(string guuid)
        //{
        //    if (string.IsNullOrWhiteSpace(guuid))
        //    {
        //        return NotFound();
        //    }

        //    var customer = await _context.Customers
        //        .FirstOrDefaultAsync(m => m.Id.ToString() == guuid);
        //    if (customer == null)
        //    {
        //        return NotFound();
        //    }

        //    return View(customer);
        //}

        //// POST: Customers/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public async Task<IActionResult> DeleteConfirmed(int id)
        //{
        //    var customer = await _context.Customers.FindAsync(id);
        //    _context.Customers.Remove(customer);
        //    await _context.SaveChangesAsync();
        //    return RedirectToAction(nameof(Index));
        //}

        //private bool CustomerExists(string guuid)
        //{
        //    return _context.Customers.Any(e => e.Id.ToString() == guuid);
        //}
    }
}
